﻿using MediatR;
using MySqlX.XDevAPI.Common;
using NPOI.Util;
using Org.BouncyCastle.Crypto;
using RBAC_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.RBAC;

namespace RBAC_Get.API.Application.Commands
{
    public class ShowPermissionHandler : IRequestHandler<ShowPermissionCommand, THE_WIND_RISES.Common.Dtos.Result>
    {
        public IBaseRepositoryGet<Permission> PermissionRep { get; set; }
        public ILogger<ShowPermissionHandler> logger;

        public ShowPermissionHandler(IBaseRepositoryGet<Permission> permissionRep, ILogger<ShowPermissionHandler> logger)
        {
            PermissionRep = permissionRep;
            this.logger = logger;
        }
        /// <summary>
        /// 绑定权限的下拉框
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<THE_WIND_RISES.Common.Dtos.Result> Handle(ShowPermissionCommand request, CancellationToken cancellationToken)
        {
            var result = new THE_WIND_RISES.Common.Dtos.Result() { Code = (int)CommonErrorCodes.Added };
            try
            {
                string ShowSql = $"select * from Permission";
                var res = PermissionRep.GetAll(ShowSql);
                if (res != null)
                {
                    result.Code = (int)CommonErrorCodes.Added;
                    result.Data = res;
                    result.Msg = "查询成功";
                    logger.LogInformation("绑定权限的下拉框成功");
                }
            }
            catch (Exception)
            {
                logger.LogError("绑定权限的下拉框失败");
                throw;
            }
            return result;
        }
    }
}
